package org.sun.sunshine.io.nio.channel;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

@Slf4j
public class FileChannelDemo {

    private static final String FILE_PATH = "/Users/yushengjun/IdeaProjects/sunshine/producer/src/main/java/org/sun/sunshine/io/nio/channel/file.txt";
    private static final String DEST_FILE_PATH = "/Users/yushengjun/IdeaProjects/sunshine/producer/src/main/java/org/sun/sunshine/io/nio/channel/file_copy.txt";

    public static void main(String[] args) {
        //读取文件
        //fileInChannel();
        //写数据到文件
        //fileOutChannel();
        //文件拷贝
        copy(FILE_PATH,DEST_FILE_PATH);

    }

    public static void fileInChannel(){
        try(FileInputStream fileInputStream=new FileInputStream(new File(FILE_PATH));
            //文件输入通道
            FileChannel fileChannel =  fileInputStream.getChannel();) {
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            StringBuffer stringBuffer = new StringBuffer();
            int len = -1;
            while((len=fileChannel.read(byteBuffer))>0){
                byteBuffer.flip();
                stringBuffer.append(new String(byteBuffer.array(), 0,len, "UTF-8"));
                byteBuffer.clear();
            }
            log.info(stringBuffer.toString());
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static void fileOutChannel(){
        Path path= Paths.get(FILE_PATH);
        try (FileChannel fileChannel = FileChannel.open(path,StandardOpenOption.APPEND)
        ){
            //append to file
            fileChannel.write(ByteBuffer.wrap("hello world".getBytes()));
            fileChannel.force(true);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void copy(String src,String dest){
        Path srcPath= Paths.get(src);
        Path destPath = Paths.get(dest);

        if (!Files.exists(srcPath))
            throw new RuntimeException("src file not exists");

        try (FileChannel srcChannel = FileChannel.open(srcPath,StandardOpenOption.READ);
             FileChannel destChannel = FileChannel.open(destPath,StandardOpenOption.WRITE,
                     StandardOpenOption.CREATE)
        ){
            destChannel.transferFrom(srcChannel,0,srcChannel.size());
            /*ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            while(srcChannel.read(byteBuffer)>0){
                byteBuffer.flip();
                destChannel.write(byteBuffer);
            }*/
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}